# +----------------------------------------------------------------------
# | ChatWork智能聊天办公系统
# +----------------------------------------------------------------------
# | 软件声明: 本系统并非自由软件,未经授权任何形式的商业使用均属非法。
# | 版权保护: 任何企业和个人不允许对程序代码以任何形式任何目的复制/分发。
# | 授权要求: 如有商业使用需求,请务必先与版权所有者取得联系并获得正式授权。
# +----------------------------------------------------------------------
# | Author: ChatWork Team <2474369941@qq.com>
# +----------------------------------------------------------------------
import time
from tortoise.expressions import Q
from hypertext import PagingResult
from common.models.draws import DrawsPromptExampleModel
from apps.admin.schemas.apply.draws import prompt_example_schema as schema


class DrawsPromptExampleService:
    """ 绘画提示词示例服务类 """

    @classmethod
    async def lists(cls, params: schema.DrawsPromptExampleSearchIn) -> PagingResult[schema.DrawsPromptExampleVo]:
        """
        提示词示例列表

        Args:
            params (schema.DrawsPromptExampleSearchIn): 搜索参数

        Returns:
            PagingResult[schema.DrawsPromptExampleVo]

        Author:
            zero
        """
        where = DrawsPromptExampleModel.build_search({
            "=": ["is_disable"],
            "%like%": ["title"],
            "datetime": ["start_time|end_time@create_time"]
        }, params.__dict__)

        where.append(Q(engine=params.engine))
        where.append(Q(is_delete=0))

        _model = DrawsPromptExampleModel.filter(is_delete=0).filter(*where).order_by("-sort", "-id")
        _pager = await DrawsPromptExampleModel.paginate(
            model=_model,
            page_no=params.page_no,
            page_size=params.page_size,
            schema=schema.DrawsPromptExampleVo,
            fields=DrawsPromptExampleModel.without_field("is_delete,delete_time")
        )

        return _pager

    @classmethod
    async def add(cls, post: schema.DrawsPromptExampleAddIn) -> None:
        """
        提示词示例新增

        Args:
            post (schema.DrawsPromptExampleAddIn): 新增参数

        Author:
            zero
        """
        await DrawsPromptExampleModel.create(
            engine=post.engine,
            title=post.title,
            content=post.content,
            sort=post.sort,
            is_disable=post.is_disable,
            create_time=int(time.time()),
            update_time=int(time.time())
        )

    @classmethod
    async def edit(cls, post: schema.DrawsPromptExampleEditIn) -> None:
        """
        提示词示例编辑

        Args:
            post (schema.DrawsPromptExampleEditIn): 编辑参数

        Author:
            zero
        """
        await DrawsPromptExampleModel.filter(id=post.id).get()
        await DrawsPromptExampleModel.filter(id=post.id).update(
            engine=post.engine,
            title=post.title,
            content=post.content,
            sort=post.sort,
            is_disable=post.is_disable,
            update_time=int(time.time())
        )

    @classmethod
    async def delete(cls, id_: int) -> None:
        """
        提示词示例删除

        Args:
            id_ (int): 分类ID

        Author:
            zero
        """
        await DrawsPromptExampleModel.filter(id=id_).get()
        await DrawsPromptExampleModel.filter(id=id_).update(
            is_delete=1,
            delete_time=int(time.time())
        )
